package com.netease.cloud.cm.common;

import com.netease.cloud.cm.util.CommonStringUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class DailySummary {
	private int round;
	private int all;
	private int success;
	private int fail;
	private List<String> times = new ArrayList<>();
	private String env;

	public DailySummary() {
	}

	public DailySummary(String env) {
		this.env = env;
	}

	public synchronized void add(int all, int success, int fail) {
		round++;
		if (fail > 0) {
			Calendar c = Calendar.getInstance();
			times.add("" + c.get(Calendar.HOUR_OF_DAY));
		}
		this.all += all;
		this.success += success;
		this.fail += fail;
	}

	public synchronized void reset() {
		round = 0;
		all = 0;
		success = 0;
		fail = 0;
		times.clear();
	}

	@Override
	public String toString() {
		// [NCM][offline][localhost] 测试 0 轮，用例 0 个，成功 0 个，失败 0 个
		String content = String.format("[%s][%s][%s] 测试 %d 轮，用例 %d 个，成功 %d 个，失败 %d 个", CommonStringUtils.getConfig("ncm.server.appname"), CommonStringUtils.getConfig("ncm.server.env"), env, round, all, success, fail);
		if (fail > 0) {
			content += String.format("，失败时间点为 [%s]", String.join(",", times));
		}
		return content;
	}

	public int getAll() {
		return all;
	}

	public void setAll(int all) {
		this.all = all;
	}

	public int getSuccess() {
		return success;
	}

	public void setSuccess(int success) {
		this.success = success;
	}

	public int getFail() {
		return fail;
	}

	public void setFail(int fail) {
		this.fail = fail;
	}

	public List<String> getTimes() {
		return times;
	}

	public void setTimes(List<String> times) {
		this.times = times;
	}

}
